package be.kdg.netpresentclient;

import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import javax.swing.*;

import be.kdg.netpresent.*;

/**
* Main class for the client application.
* 
* @author Joris Clevenbergs, Timothy Huber, Rik Verschuere, Wim Van der Wilt
*/
public class Main {
	public static void main(String[] args) {
		try {
			// For the ease of using and testing the application, a popup at the
			// beginning will ask for the IP of the server. This can be "localhost"
			// for example if testing it locally.
			String sServerIP = getServerIPfromUser();

			// After obtaining the address, build up the RMI connection.
			NetPresentInt serverObject = (NetPresentInt) LocateRegistry
					.getRegistry(sServerIP, 2021).lookup("/NetPresent");

			// Next create a popup for the client user to be able to choose from
			// a list of available presentations.
			ClientFirstChoice cfcInst0 = new ClientFirstChoice(serverObject);
			String sPresentationName = cfcInst0.getUserChoice();

			int iPresNum = serverObject.getPresIndexByName(sPresentationName);

			if (0 > iPresNum) {
				System.out.println("Computer says NO!");
				while (true);
			}

			String sPWD = showPasswordField(false, sPresentationName);

			// Getting the presentation with the correct password
			PresentationInt presInst0 = (PresentationInt) serverObject
					.getPresentation(iPresNum, sPWD);

			// First password was incorrect, loop until password is entered correctly!
			while (null == presInst0) {
				sPWD = showPasswordField(true, sPresentationName);

				presInst0 = (PresentationInt) serverObject.getPresentation(
						iPresNum, sPWD);
			}

			ClientGUI client = new ClientGUI(presInst0);
			client.setVisible(true);

			client.startPresentation();

		} catch (RemoteException re) {
			System.out.println();
			System.out.println("RemoteException");
			System.out.println(re);
		} catch (NotBoundException nbe) {
			System.out.println();
			System.out.println("NotBoundException");
			System.out.println(nbe);
		} catch (java.lang.ArithmeticException ae) {
			System.out.println();
			System.out.println("java.lang.ArithmeticException");
			System.out.println(ae);
		} catch (Exception e) {
			System.out.println();
			System.out.println("Exception");
			System.out.println(e);
		}
	}

	/**
	* Creates a message box to fill in the servers IP address 
	*/
	private static String getServerIPfromUser() {
		// Voluntary try-catch to catch NullPointerExceptions due to canceling
		// the input-dialog as a whole.
		String sServerIP = null;

		try {
			sServerIP = JOptionPane.showInputDialog(null,
					("Geef het IP-adres van de server "
							+ "om de presentaties te laden"),
					"NetPresent - Client Connect", 1);

			while (sServerIP.length() == 0) {
				sServerIP = JOptionPane.showInputDialog(null,
						("Error: IP kan niet leeg zijn!\n"
								+ "Geef het IP-adres van de server"
								+ "om de presentaties te laden:"),
						"NetPresent - Client Connect", 1);
			}
		} catch (NullPointerException e) {
			System.out.println("IP input gecancelled");
			return null;
		}

		return sServerIP;
	}
	
	/**
	* Creates a message box to fill in the password 
	*
	* @param flag	Boolean which is true if you already gave an incorrect password. 
	* @param sPresentationName	String which holds the name of the presentation.
	*/
	private static String showPasswordField(boolean flag,
			String sPresentationName) {
		JPasswordField passwordField = new JPasswordField();
		passwordField.setEchoChar('*');

		Object[] objInst0 = {
				("Geef het paswoord voor de presentatie: \""
						+ sPresentationName + "\""), passwordField };

		if (flag) {
			objInst0[0] = "Dit paswoord is verkeerd!\n"
					+ "Geef het correcte paswoord voor de presentatie: \""
					+ sPresentationName + "\"";

		}

		String[] sPWDOptions = { "Log in" };

		JOptionPane
				.showOptionDialog(null, objInst0, "Geef het paswoord aub...",
						JOptionPane.OK_OPTION, JOptionPane.QUESTION_MESSAGE,
						null, sPWDOptions, sPWDOptions[0]);

		String sPWD = String.valueOf(passwordField.getPassword());

		return sPWD;
	}
}
